package com.guoqi.marrydate

import android.annotation.SuppressLint
import android.app.PendingIntent
import android.appwidget.AppWidgetManager
import android.appwidget.AppWidgetProvider
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.widget.RemoteViews
import com.guoqi.marrydate.flag.FlagBean
import org.litepal.LitePal
import java.text.SimpleDateFormat
import java.util.*


class MyAppWidgetProvider : AppWidgetProvider() {
    val TAG = this.javaClass.simpleName

    private var mRemoteViews: RemoteViews? = null

    override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
        super.onUpdate(context, appWidgetManager, appWidgetIds)
        for (appWidgetId in appWidgetIds) {
            updateAppWidget(this, context, appWidgetManager, appWidgetId)
        }
    }

    override fun onAppWidgetOptionsChanged(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int, newOptions: Bundle) {
        Log.e(TAG, "当 widget 被初次添加 或者 当 widget 的大小被改变时，被调用")
        super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions)
    }

    override fun onDeleted(context: Context, appWidgetIds: IntArray) {
        Log.e(TAG, "widget被删除时调用")
        super.onDeleted(context, appWidgetIds)
    }

    override fun onEnabled(context: Context) {
        super.onEnabled(context)
        Log.e(TAG, "onEnabled:第一个widget被创建时调用")
        var intent = Intent()
        intent.action = "WIDGET_CREATE_FIRST"
        intent.setPackage(context.packageName)
        context.startService(intent)
    }

    override fun onDisabled(context: Context) {
        Log.e(TAG, "onDisabled:最后一个widget被删除时调用")
        super.onDisabled(context)
    }


    @SuppressLint("SimpleDateFormat")
    override fun onReceive(context: Context, intent: Intent) {
        val mgr = AppWidgetManager.getInstance(context)
        val cn = ComponentName(context, MyAppWidgetProvider::class.java)
        Log.e(TAG, "OnReceive:Action: " + intent.action)
        mRemoteViews = RemoteViews(context.packageName, R.layout.widget_layout)

        var list = LitePal.where("1=1 and date>='" + SimpleDateFormat("yyyy-MM-dd").format(Date()) + "'").order("sortNo desc,date asc").find(FlagBean::class.java) as ArrayList
        if (!list.isNullOrEmpty()) {
            val bean = list[0]
            var statusStr = if (bean.sortNo == 50) {
                "重要"
            } else {
                "待办"
            }
            mRemoteViews?.setTextViewText(R.id.tv_status, statusStr)
            mRemoteViews?.setTextViewText(R.id.tv_title, bean.title)

            var sdf = SimpleDateFormat("yyyy-MM-dd")
            var futureDate = bean.date + " 00:00:00"
            var nowDate = sdf.format(Date()) + " 00:00:00"
            val diffDays = ((sdf.parse(futureDate).time - sdf.parse(nowDate).time) / (1000 * 3600 * 24)).toInt()
            when (diffDays) {
                0 -> {
                    mRemoteViews?.setTextViewText(R.id.tv_last, "今天")
                    mRemoteViews?.setTextColor(R.id.tv_last, Color.parseColor("#FF0000"))
                }
                1 -> {
                    mRemoteViews?.setTextViewText(R.id.tv_last, "明天")
                    mRemoteViews?.setTextColor(R.id.tv_last, Color.parseColor("#FF5511"))
                }
                2 -> {
                    mRemoteViews?.setTextViewText(R.id.tv_last, "后天")
                    mRemoteViews?.setTextColor(R.id.tv_last, Color.parseColor("#FF8800"))
                }
                else -> mRemoteViews?.setTextViewText(R.id.tv_last, "${diffDays.toInt()}天")
            }
            mgr.updateAppWidget(cn, mRemoteViews)
        }

        super.onReceive(context, intent)
    }

    companion object {
        val CHANGE_DATA = "com.guoqi.marrydate.action.CHANGE_DATA"
        @JvmStatic
        fun updateAppWidget(myAppWidgetProvider: MyAppWidgetProvider, context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int) {
            Log.e(myAppWidgetProvider.TAG, "updateAppWidget")
            myAppWidgetProvider.mRemoteViews = RemoteViews(context.packageName, R.layout.widget_layout)

            //打开主页
            val skipIntent = Intent(context, MainActivity::class.java)
            val pi = PendingIntent.getActivity(context, 0, skipIntent, PendingIntent.FLAG_CANCEL_CURRENT)
            myAppWidgetProvider.mRemoteViews?.setOnClickPendingIntent(R.id.ll_date, pi)

            //更新
            appWidgetManager.updateAppWidget(appWidgetId, myAppWidgetProvider.mRemoteViews)
        }
    }

}